
package com.insigma.business.qrcode.soldier._provider.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;
import com.beust.jcommander.internal.Sets;
import com.insigma.business.common.utils.ToolsHelper;
import com.insigma.business.qrcode.common.converter.SoldierApplyInfoProviderConverter;
import com.insigma.business.qrcode.common.dao.CommonAdminAreaDO;
import com.insigma.business.qrcode.common.dto.SoldierApplyTypeEnum;
import com.insigma.business.qrcode.common.dto.SoldierTypeEnum;
import com.insigma.business.qrcode.common.service.CommonAdminAreaService;
import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.*;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.ProcessStatusEnum;
import com.insigma.business.qrcode.intell.service.CareSoldierWelfareApplyInfoService;
import com.insigma.business.qrcode.soldier._provider.SoldierProvider;
import com.insigma.business.qrcode.soldier.dao.SoldierApplyInfoDao;
import com.insigma.business.qrcode.soldier.dao.SoldierBasicInfoDao;
import com.insigma.business.qrcode.soldier.dto.*;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.soldier.entity.*;
import com.insigma.business.qrcode.soldier.request.BaseApiRequest;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.service.*;
import com.insigma.business.qrcode.common.converter.SoldierProviderConverter;
import com.insigma.business.qrcode.soldier.util.BizCheckUtil;
import com.insigma.business.qrcode.welfare.dto.SoldierHoldInfoDTO;
import com.insigma.business.qrcode.welfare.dto.SoldierUserInfoDTO;
import com.insigma.business.qrcode.common.dto.ErrorCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Slf4j
@Service
@Component("soldierProvider")
public class SoldierProviderImpl extends BaseProviderImpl implements SoldierProvider {

    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    @Autowired
    @Lazy
    private SoldierService soldierService;

    @Autowired
    private CommonAdminAreaService commonAdminAreaService;

    @Autowired(required = false)
    private SoldierBasicInfoDao soldierBasicInfoDao;

    @Autowired(required = false)
    private SoldierApplyInfoDao soldierApplyInfoDao;

    @Autowired
    private SoldierOuterBasicInfoService soldierOuterBasicInfoService;

    @Autowired
    private SoldierBasicInfoService soldierBasicInfoService;

    @Autowired
    private GovResourceService govResourceService;

    @Autowired
    private SoldierApplyInfoService soldierApplyInfoService;

    @Autowired
    private SoldierDailyLoginService soldierDailyLoginService;

    @Resource
    CodeEngineService codeEngineServiceRedis;

    @Autowired
    private CareSoldierWelfareApplyInfoService careSoldierWelfareApplyInfoService;

    @Resource
    SoldierOuterLoginAccountService soldierOuterLoginAccountService;


    @Override
    public BaseResponse<SoldierUserInfoDTO> checkSshUserInfo(String soldierId) {

        return getSoldierUserInfoDTOBaseResponse(soldierId, null);
    }

    @Override
    public BaseResponse<List<SoldierUserInfoDTO>> querySoldierRelationshipList(String soldierId) {
        BizCheckUtil.checkNotBlank(soldierId, "soldierId不能为空");
        //校验老兵信息
        SoldierApplyInfoDO soldierApplyInfoDO = soldierApplyInfoService.getBySoldierId(soldierId);
        if (soldierApplyInfoDO == null ) {
            throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, "未查到该老兵ID的申请信息");
        }
        if (!ProcessStatusEnum.SUCCESS.name().equals(soldierApplyInfoDO.getStatus())) {
            throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR, "该老兵码申请状态不正确");
        }
        //判断老兵码申请信息，如果为本人则查询代持数据，如果为代持人查询本人数据

        //定义出参
        List<SoldierUserInfoDTO> result = new ArrayList<>();
        //第一条数据设置为代持关系为本人
        soldierApplyInfoDO.setHoldRelationship("本人");
        SoldierUserInfoDTO soldierUserInfoDTO = new SoldierUserInfoDTO();
        BeanUtils.copyProperties(soldierApplyInfoDO, soldierUserInfoDTO);
        result.add(soldierUserInfoDTO);

        //查询子女代申请的数据
        SoldierApplyInfoBSearcher soldierApplyInfoBSearcher = new SoldierApplyInfoBSearcher(1, 1000);
        soldierApplyInfoBSearcher.setHolded(true);
        soldierApplyInfoBSearcher.setHoldIdCardNo(soldierApplyInfoDO.getIdCardNo());
        soldierApplyInfoBSearcher.setStatus(ProcessStatusEnum.SUCCESS.name());
        List<SoldierApplyInfoDO> soldierApplyInfoHoldList = soldierApplyInfoService.search(soldierApplyInfoBSearcher);
        if (!CollectionUtils.isEmpty(soldierApplyInfoHoldList)) {
            List<SoldierUserInfoDTO> soldierUserInfoResultList = ToolsHelper.copyListProperties(soldierApplyInfoHoldList, SoldierUserInfoDTO::new);
            result.addAll(soldierUserInfoResultList);
        }
        return ResponseUtils.buildSuccessResponse(result);
    }

    private BaseResponse<SoldierUserInfoDTO> getSoldierUserInfoDTOBaseResponse(String soldierId, Boolean old) {
        BizCheckUtil.checkNotBlank(soldierId, "soldierId不能为空");

        SoldierUserInfoDTO result = new SoldierUserInfoDTO();

        SoldierApplyInfoDO soldierApplyInfoDO = soldierApplyInfoService.getBySoldierId(soldierId);
        if (soldierApplyInfoDO == null) {
            result.setStatus(SoldierStatusEnum.NULL.getTitle());
            return ResponseUtils.buildSuccessResponse(result);
        }

        SoldierApplyInfoDTO soldierApplyInfoDTO = SoldierApplyInfoProviderConverter.convertResponseDO2DTO(soldierApplyInfoDO);
        DecryptDTOUtils.decryptSoldierApplyInfoDTO(soldierApplyInfoDTO);

        result.setSoldierApplyInfoDTO(soldierApplyInfoDTO);
        result.setSoldierType(soldierApplyInfoDTO.getSoldierType());
        String status = soldierApplyInfoDO.getStatus();
        if (status.equals(ProcessStatusEnum.DEALING.name())) {
            result.setStatus(SoldierStatusEnum.DEALING.getTitle());
        } else if (status.equals(ProcessStatusEnum.SUCCESS.name())) {
            //查询代持信息列表
            List<SoldierHoldInfoDTO> soldierHoldInfoList = this.querySoldierHoldInfoList(soldierApplyInfoDO);
            result.setSoldierHoldInfoList(soldierHoldInfoList);
            //查询基本信息
            SoldierBasicInfoDO base = soldierBasicInfoDao.getBySoldierId(soldierId);
            BeanUtils.copyProperties(base, result);
            result.setStatus(SoldierStatusEnum.SUCCESS.getTitle());

            CodeEngineSoldierBasicInfo soldierQr = null;
            if (Boolean.TRUE.equals(old)) {
                soldierQr = getSoldierQrOld(base, soldierApplyInfoDO);
            } else {
                soldierQr = getSoldierQr(base, soldierApplyInfoDO);
            }

            result.setQrString(soldierQr.getCode());
            result.setSoldierType(base.getSoldierType());
            result.setLevel(soldierQr.getLevel());
            result.setAvatarPath(base.getAvatarPath());
            result.setHolded(soldierApplyInfoDO.getHolded());
            result.setHoldName(soldierApplyInfoDO.getHoldName());
            result.setHoldGender(soldierApplyInfoDO.getHoldGender());
            result.setHoldIdCardNo(soldierApplyInfoDO.getHoldIdCardNo());
            result.setHoldBirthday(soldierApplyInfoDO.getHoldBirthday());
            result.setHoldRelationship(soldierApplyInfoDO.getHoldRelationship());
            result.setHoldMobile(soldierApplyInfoDO.getHoldMobile());
            result.setColour(soldierQr.getColour());
            result.setServDura(DateUtils.yearBetween(base.getEntryDatetime(),base.getRetireDatetime()));
        } else if (status.equals(ProcessStatusEnum.REJECT.name())) {
            result.setStatus(ProcessStatusEnum.REJECT.name());
        }

        addDailyRecordAndFillInfo(result, soldierApplyInfoDO.getStatus());
        return ResponseUtils.buildSuccessResponse(result);
    }

    private void addDailyRecordAndFillInfo(SoldierUserInfoDTO result, String status) {
        if (SoldierStatusEnum.SUCCESS.getTitle().equals(result.getStatus()) || SoldierStatusEnum.SUCCESS.getTitle().equals(status)) {

            SoldierDailyLoginDO soldierDailyLoginDO = soldierDailyLoginService.addIfFirstAndGet(result.getSoldierId(), result.getRealName(), result.getIdCardNo());
            result.setLoginDays(soldierDailyLoginDO.getLoginDays());
            result.setIntegral(soldierDailyLoginDO.getIntegral());
        }
    }

    @Override
    public BaseResponse<SoldierUserInfoDTO> checkSshUserInfoOldQrString(String soldierId) {
        return getSoldierUserInfoDTOBaseResponse(soldierId, true);
    }

    private boolean checkIsIn15m(Date createDatetime) {
        long between = DateUtil.between(createDatetime, new Date(), DateUnit.MINUTE);
        if (between < 15) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        long between = DateUtil.between(new Date(System.currentTimeMillis() + 60 * 15 * 1000), new Date(), DateUnit.MINUTE);
        System.out.println();
    }

    private boolean checkTemporaryQrIsExpired(SoldierApplyInfoDO soldierApplyTemp) {
        int i = DateUtils.daysBetween(soldierApplyTemp.getCreateDatetime(), new Date());
        if (i > 7) {
            return true;
        }

        if ("DELETE".equals(soldierApplyTemp.getApplyStatus())) {
            return true;
        }
        return false;
    }

    @Override
    public BaseResponse<SoldierUserInfoDTO> checkLbmInfo(String soldierId) {
        SoldierBasicInfoDO soldierDO = soldierBasicInfoService.getBySoldierId(soldierId);
        log.info("checkLbmInfo:soldierId={}", soldierId);
        log.info("checkLbmInfo:soldierDO={}", soldierDO);
        if (soldierDO == null) {
            throw new BizException(ErrorCodeEnum.ERROR_USER_NOT_HAS_LBM);
        }

        SoldierApplyInfoDO soldierApplyInfoDO = soldierApplyInfoService.getBySoldierId(soldierId);
        log.info("checkLbmInfo:soldierApplyInfoDO={}", soldierApplyInfoDO);
        if (soldierApplyInfoDO == null || !soldierApplyInfoDO.getStatus().equals(ProcessStatusEnum.SUCCESS.name())) {
            SoldierUserInfoDTO result = new SoldierUserInfoDTO();

            List<SoldierApplyInfoDO> soldierApplyInfoDOS = soldierApplyInfoService.listBySoldierId(soldierId, SoldierApplyTypeEnum.OUTSIDER.name());
            log.info("checkLbmInfo:soldierApplyInfoDOS={}", soldierApplyInfoDOS);
            if (!CollectionUtils.isEmpty(soldierApplyInfoDOS)) {
                SoldierApplyInfoDO outerApplyDO = soldierApplyInfoDOS.get(0);
                if (outerApplyDO.getStatus().equals(ProcessStatusEnum.SUCCESS.name())) {

                    SoldierOuterBasicInfoDO base = soldierOuterBasicInfoService.getBySoldierId(soldierId);
                    BeanUtils.copyProperties(soldierDO, result);
                    BeanUtils.copyProperties(outerApplyDO, result);
                    BeanUtils.copyProperties(base, result);
                    SoldierBasicInfoDO baseTmp = new SoldierBasicInfoDO();
                    BeanUtils.copyProperties(base, baseTmp);
                    CodeEngineSoldierBasicInfo soldierQr = getSoldierQr(baseTmp, outerApplyDO);

                    result.setRealName(base.getRealName());
                    result.setIdCardNo(base.getIdCardNo());
                    result.setSoldierType(base.getSoldierType());
                    result.setQrString(soldierQr.getCode());
                    result.setAvatarPath(base.getAvatarPath());
                    result.setColour(soldierQr.getColour());
                    result.setApplyType(SoldierApplyTypeEnum.OUTSIDER.name());
                    return ResponseUtils.buildSuccessResponse(result);
                }

            }


            SoldierApplyInfoDO soldierApplyTemp = soldierApplyInfoService.getLastApplyOfSoldierId(soldierId, SoldierApplyTypeEnum.TEMPORARY.name());
            log.info("checkLbmInfo:soldierApplyTemp={}", soldierApplyTemp);
            if (soldierApplyTemp != null) {
                if (!checkTemporaryQrIsExpired(soldierApplyTemp) && !checkIsIn15m(soldierApplyTemp.getCreateDatetime())) {
                    BeanUtils.copyProperties(soldierDO, result);
                    BeanUtils.copyProperties(soldierApplyTemp, result);
                    SoldierBasicInfoDO baseTmp = new SoldierBasicInfoDO();
                    BeanUtils.copyProperties(soldierApplyTemp, baseTmp);
                    CodeEngineSoldierBasicInfo soldierQr = getSoldierQr(baseTmp, soldierApplyTemp);

                    result.setQrString(soldierQr.getCode());
                    result.setColour(soldierQr.getColour());
                    result.setApplyType(SoldierApplyTypeEnum.TEMPORARY.name());
                    return ResponseUtils.buildSuccessResponse(result);
                }
            }

            throw new BizException(ErrorCodeEnum.ERROR_USER_NOT_HAS_LBM);
        }

        SoldierUserInfoDTO result = new SoldierUserInfoDTO();
        BeanUtils.copyProperties(soldierApplyInfoDO, result);
        BeanUtils.copyProperties(soldierDO, result);
        SoldierBasicInfoDO base = soldierBasicInfoDao.getBySoldierId(soldierId);
        log.info("checkLbmInfo:base={}", base);
        CodeEngineSoldierBasicInfo soldierQr = getSoldierQr(base, soldierApplyInfoDO);

        result.setRealName(base.getRealName());
        result.setIdCardNo(base.getIdCardNo());
        result.setSoldierType(base.getSoldierType());
        result.setSoldierTypeDictCode(base.getSoldierTypeDictCode());
        result.setQrString(soldierQr.getCode());
        result.setAvatarPath(base.getAvatarPath());
        result.setHolded(soldierApplyInfoDO.getHolded());
        result.setHoldName(soldierApplyInfoDO.getHoldName());
        result.setHoldGender(soldierApplyInfoDO.getHoldGender());
        result.setHoldIdCardNo(soldierApplyInfoDO.getHoldIdCardNo());
        result.setHoldBirthday(soldierApplyInfoDO.getHoldBirthday());
        result.setHoldRelationship(soldierApplyInfoDO.getHoldRelationship());
        result.setHoldMobile(soldierApplyInfoDO.getHoldMobile());
        result.setColour(soldierQr.getColour());
        result.setResidentAdminAreaCode(base.getResidentAdminAreaCode());

        addDailyRecordAndFillInfo(result, soldierApplyInfoDO.getStatus());
        return ResponseUtils.buildSuccessResponse(result);
    }


    private CodeEngineSoldierBasicInfo getSoldierQr(SoldierBasicInfoDO base, SoldierApplyInfoDO soldierApplyInfoDO) {
        if (base == null) {
            return null;
        }

        CodeEngineSoldierBasicInfo ceBasicInfo = new CodeEngineSoldierBasicInfo();
        BeanUtils.copyProperties(base, ceBasicInfo);
        ceBasicInfo.setPreferential(0);
        String colour = buildColour(base, soldierApplyInfoDO);
        ceBasicInfo.setColour(colour);
        CodeEngineResponse codeEngineResponse = codeEngineServiceRedis.queryQrString(ceBasicInfo);
        if (codeEngineResponse == null) {
            CodeEngineResponse redisResponse = codeEngineServiceRedis.queryQrString(ceBasicInfo);
            CodeEngineSoldierBasicInfo data = redisResponse.getData();
            data.setColour(colour);
            return data;
        }
        CodeEngineSoldierBasicInfo data = codeEngineResponse.getData();
        data.setColour(colour);
        return data;
    }


    private CodeEngineSoldierBasicInfo getSoldierQrOld(SoldierBasicInfoDO base, SoldierApplyInfoDO soldierApplyInfoDO) {
        if (base == null) {
            return null;
        }

        CodeEngineSoldierBasicInfo ceBasicInfo = new CodeEngineSoldierBasicInfo();
        BeanUtils.copyProperties(base, ceBasicInfo);
        ceBasicInfo.setPreferential(0);
        String colour = buildColour(base, soldierApplyInfoDO);
        ceBasicInfo.setColour(colour);
        CodeEngineResponse codeEngineResponse = codeEngineServiceRedis.queryQrString(ceBasicInfo);
        if (codeEngineResponse == null) {
            CodeEngineResponse redisResponse = codeEngineServiceRedis.queryQrString(ceBasicInfo);
            CodeEngineSoldierBasicInfo data = redisResponse.getData();
            data.setColour(colour);
            return data;
        }
        CodeEngineSoldierBasicInfo data = codeEngineResponse.getData();
        data.setColour(colour);
        return data;
    }

    /**
     * 11:10
     * #65b076   绿色  默认
     * #93a6f7    蓝色  双拥
     * #B36741  棕色  代持
     * ##f57600  黄色
     *
     * @param base
     * @param applyInfoDO
     * @return
     */
    private String buildColour(SoldierBasicInfoDO base, SoldierApplyInfoDO applyInfoDO) {
        if (applyInfoDO.getHolded()) {
            return "#ff9d42";
        }

        if (isPreferential(base.getSoldierType(), CommonConstant.PREFERENTIAL_TYPE)) {
            return "#dd8f89";
        }

        if (SoldierTypeEnum.ACTIVE_DUTY_SOLDIER.name().equals(base.getSoldierType())) {
            return "#388f88";
        }

        return "#286043";
    }

    private boolean isPreferential(String soldierType, List<String> preferentialType) {
        if (Strings.isBlank(soldierType)) {
            return false;
        }
        String[] split = soldierType.split(",");
        for (String s : split) {
            if (preferentialType.contains(s.trim())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public BaseResponse<SoldierUserInfoDTO> querySoldierDetail(String soldierId) {

        BizCheckUtil.checkNotBlank(soldierId, "soldierId不能为空");

        SoldierDO soldierDO = soldierService.get(soldierId);
        soldierDO.setPassword(null);


        SoldierApplyInfoDO applyInfoDO = soldierApplyInfoService.getLastApplyOfSoldierId(soldierId, null);

        SoldierBasicInfoDO soldierBasicInfoDO = soldierBasicInfoService.getByQrSuccess(soldierId);
        BizCheckUtil.checkNotNull(soldierBasicInfoDO, "还未获得杭州老兵码！");

        SoldierUserInfoDTO soldierUserInfoDTO = new SoldierUserInfoDTO();
        BeanUtils.copyProperties(soldierDO, soldierUserInfoDTO);
        if (soldierBasicInfoDO != null) {
            BeanUtils.copyProperties(soldierBasicInfoDO, soldierUserInfoDTO);
            soldierUserInfoDTO.setRealName(soldierBasicInfoDO.getRealName());
            soldierUserInfoDTO.setHolded(applyInfoDO.getHolded());
            soldierUserInfoDTO.setHoldName(applyInfoDO.getHoldName());
            soldierUserInfoDTO.setHoldGender(applyInfoDO.getHoldGender());
            soldierUserInfoDTO.setHoldIdCardNo(applyInfoDO.getHoldIdCardNo());
            soldierUserInfoDTO.setHoldBirthday(applyInfoDO.getHoldBirthday());
            soldierUserInfoDTO.setHoldRelationship(applyInfoDO.getHoldRelationship());
            soldierUserInfoDTO.setHoldMobile(applyInfoDO.getHoldMobile());

            CodeEngineSoldierBasicInfo soldierQr = getSoldierQr(soldierBasicInfoDO, applyInfoDO);
            soldierUserInfoDTO.setQrString(soldierQr.getCode());
            soldierUserInfoDTO.setColour(soldierQr.getColour());
            CareSoldierWelfareApplyInfoDO welfareApplyInfoDO = careSoldierWelfareApplyInfoService.getSuccByIdCardNoAndSource(soldierBasicInfoDO.getIdCardNo(), WelfareApplySourceEnum.CPH.name());
            if (welfareApplyInfoDO != null) {
                soldierUserInfoDTO.setCarNo(welfareApplyInfoDO.getContent());
            }
        }

        return ResponseUtils.buildSuccessResponse(soldierUserInfoDTO);
    }

    @Override
    public BaseResponse<SoldierUserInfoDTO> querySoldierDetailByQrString(String qrString) {

        BizCheckUtil.checkNotBlank(qrString, "入参不能为空");

        CodeEngineResponse codeEngineResponse = codeEngineServiceRedis.queryBasicInfoByCode(qrString);
        if (codeEngineResponse == null || codeEngineResponse.getData() == null) {
            return ResponseUtils.buildFailResponse(ErrorCodeEnum.ERROR_QR);
        }

        SoldierBasicInfoDO soldierBasicInfoDO = soldierBasicInfoDao.getByIdCardNo(codeEngineResponse.getData().getIdCardNo());

        SoldierUserInfoDTO soldierUserInfoDTO = new SoldierUserInfoDTO();
        BeanUtils.copyProperties(soldierBasicInfoDO, soldierUserInfoDTO);

        return ResponseUtils.buildSuccessResponse(soldierUserInfoDTO);
    }

    @Override
    public BaseResponse<SoldierDTO> getSoldier(final BaseGetRequest baseGetRequest) {

        final BaseResponse<SoldierDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条Soldier的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final SoldierDO soldierDO = soldierService.get((String) baseGetRequest.getId());
                final SoldierDTO remoteObj = SoldierProviderConverter.convertResponseDO2DTO(soldierDO);
                ResponseUtils.setupSuccessResponse(baseResponse, remoteObj);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_GET;
            }
        });
        return baseResponse;
    }



    private Set<String> recursiveGetParentResource(GovResourceDO govResourceDO) {
        if (govResourceDO == null || Strings.isBlank(govResourceDO.getParentCode())) {
            return Sets.newHashSet();
        }

        Set<String> set = Sets.newHashSet();
        String code = govResourceDO.getParentCode();
        GovResourceDO byCode = govResourceService.getByCode(code);

        set.add(byCode.getCode());
        set.addAll(recursiveGetParentResource(byCode));

        return set;
    }










    @Override
    public BaseResponse<Boolean> updateRealNameIfMobileExisted(String soldierId, String mobile, String userName) {

        BizCheckUtil.checkNotBlank(soldierId, "soldierId不能为空");
        BizCheckUtil.checkMobile(mobile);
        BizCheckUtil.checkIsTrue(!userName.contains("1"), "username错误");

        Boolean result = soldierOuterLoginAccountService.updateRealNameIfMobileExisted(soldierId, mobile, userName);
        return ResponseUtils.buildSuccessResponse(result);
    }

    @Override
    public void testSoldierType() {
        Map<String, String> name2TitleMap = SoldierTypeEnum.getName2TitleMap();

        Map<String, String> titleMap = Maps.newHashMap();
        for (String s : name2TitleMap.keySet()) {
            String s1 = name2TitleMap.get(s);
            titleMap.put(s1, s);
        }

        int i = 1;
        List<SoldierBasicInfoDO> soldierBasicInfoDOS = soldierBasicInfoDao.allSoldier();
        for (SoldierBasicInfoDO soldierBasicInfoDO : soldierBasicInfoDOS) {
            if (Strings.isBlank(soldierBasicInfoDO.getSoldierTypeDictCode())) {
                String soldierType = soldierBasicInfoDO.getSoldierType();
                if (Strings.isNotBlank(soldierType)) {
                    List<String> typeList = Lists.newArrayList();
                    for (String s : soldierType.split(",")) {
                        typeList.add(titleMap.get(s));
                    }
                    soldierBasicInfoDO.setSoldierTypeDictCode(String.join(",", typeList));
                    soldierBasicInfoDao.update(soldierBasicInfoDO);
                    System.out.println(i++);
                    System.out.println(soldierBasicInfoDO.getId());
                }
            }
        }
    }






    private Date yearToDate(String str) {
        if (Strings.isEmpty(str)) {
            return null;
        }
        DateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        try {
            return fmt.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    private String dealWithType(String type) {
        String a = type.replace("和", ",");
        String[] split = a.split(",");
        ArrayList<String> objects = new ArrayList<>();
        for (String s : split) {
            objects.add(s.trim());
        }
        String join = String.join(",", split);
        return join;
    }

    /**
     * 查询代持信息列表
     * 判断是本人还是代持人
     * 本人: 设置老兵码信息为本人添加到列表，查询代持老兵码信息添加到列表
     * 代持人：用代持证件号码查询本人老兵码信息添加到列表，查询代持老兵码信息添加到列表
     */
    private List<SoldierHoldInfoDTO> querySoldierHoldInfoList(SoldierApplyInfoDO soldierApplyInfoDO) {
        List<SoldierHoldInfoDTO> result = new ArrayList<>();
        if (Boolean.FALSE.equals(soldierApplyInfoDO.getHolded())) {
            SoldierHoldInfoDTO soldierHoldInfoDTO = new SoldierHoldInfoDTO();
            BeanUtils.copyProperties(soldierApplyInfoDO, soldierHoldInfoDTO);
            soldierHoldInfoDTO.setHoldRelationship("本人");
            result.add(soldierHoldInfoDTO);
            //查询代持老兵码列表
            SoldierApplyInfoBSearcher soldierApplyInfoBSearcher = new SoldierApplyInfoBSearcher(1, 1000);
            soldierApplyInfoBSearcher.setHolded(true);
            soldierApplyInfoBSearcher.setHoldIdCardNo(soldierApplyInfoDO.getIdCardNo());
            soldierApplyInfoBSearcher.setStatus(ProcessStatusEnum.SUCCESS.name());
            List<SoldierApplyInfoDO> soldierApplyInfoList = soldierApplyInfoService.search(soldierApplyInfoBSearcher);
            if (!CollectionUtils.isEmpty(soldierApplyInfoList)) {
                List<SoldierHoldInfoDTO> soldierHoldInfoList = ToolsHelper.copyListProperties(soldierApplyInfoList, SoldierHoldInfoDTO::new);
                result.addAll(soldierHoldInfoList);
            }
        } else {
            //先查询本人信息
            SoldierApplyInfoDO soldierApplyPsnInfoDO = soldierApplyInfoService.getByIdCardInfo(soldierApplyInfoDO.getHoldIdCardNo());
            if (ProcessStatusEnum.SUCCESS.name().equals(soldierApplyPsnInfoDO.getStatus())) {
                SoldierHoldInfoDTO soldierHoldInfoDTO = new SoldierHoldInfoDTO();
                BeanUtils.copyProperties(soldierApplyPsnInfoDO, soldierHoldInfoDTO);
                soldierHoldInfoDTO.setHoldRelationship("本人");
                result.add(soldierHoldInfoDTO);
            }
            //查询代持老兵码列表
            SoldierApplyInfoBSearcher soldierApplyInfoBSearcher = new SoldierApplyInfoBSearcher(1, 1000);
            soldierApplyInfoBSearcher.setHolded(true);
            soldierApplyInfoBSearcher.setHoldIdCardNo(soldierApplyInfoDO.getHoldIdCardNo());
            soldierApplyInfoBSearcher.setStatus(ProcessStatusEnum.SUCCESS.name());
            List<SoldierApplyInfoDO> soldierApplyInfoList = soldierApplyInfoService.search(soldierApplyInfoBSearcher);
            if (!CollectionUtils.isEmpty(soldierApplyInfoList)) {
                List<SoldierHoldInfoDTO> soldierHoldInfoList = ToolsHelper.copyListProperties(soldierApplyInfoList, SoldierHoldInfoDTO::new);
                result.addAll(soldierHoldInfoList);
            }
        }
        return result;
    }
}
